<?php
/**
 * Redirect to local page
 *
 * @param string path
 *	Relative path to destination page
 */
function Redirect($url) {
	header('Location: ' . $url);
	die();
}

/**
 * Truncate long text
 *
 * @return truncated text
 */
function Truncate($str, $limited = 30) {
   $res = $str;
   if (strlen($str) > $limited) {
       preg_match('/(.{' . $limited . '}.*?)\b/', $str, $matches);
       $res = rtrim($matches[1]) . '...';
   }
   return $res;
}

/**
 * Get html error tag
 *
 * @return html
 */
function ErrorTag($text) {
	return "<span class=\"error\">$text</span>";
}

function GetValue($fields, $name) {
	if (array_key_exists($name, $fields))
		return $fields[$name];
	return '';
}

function GetCheck($fields, $name, $val) {
	if (array_key_exists($name, $fields) && $fields[$name] == $val)
		return ' checked="checked"';
	return '';
}

function GetSelect($fields, $name, $val) {
	if (array_key_exists($name, $fields) && $fields[$name] == $val)
		return ' selected="selected"';
	return '';
}

function GetError($fields, $name) {
	if (array_key_exists($name, $fields))
		return ErrorTag($fields[$name]);
	return '';
}

function GetErrorClass($fields, $name) {
	if (array_key_exists($name, $fields))
		return ' class="error"';
	return '';
}

function EchoHeader($title = 'Quản lý nhân viên', $curMenu = 'home') {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<!-- jquery  -->
	<script type="text/javascript" src="script/jquery/js/jquery-1.7.1.min.js"></script>
	<script type="text/javascript" src="script/jquery/js/jquery-ui-1.8.18.custom.min.js"></script>
	<link type="text/css" href="script/jquery/css/smoothness/jquery-ui-1.8.18.custom.css" rel="stylesheet" />	

	<!-- common -->
	<link href="style/common.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="script/sprintf-0.7-beta1.js"></script>
	<script type="text/javascript" src="script/common.js"></script>

	<title><?php echo $title; ?></title>
</head>
<body>
<?php
}

function EchoFooter() {
?>
</body>
</html>
<?php	
}

function GetUploadedFile($name) {
	if ($_FILES[$name]['name'] == '') return '';

	date_default_timezone_set("Asia/Ho_Chi_Minh");
	$localPath = "../images/upload/" . date("ymd-His-") . $_FILES[$name]['name'];
	$serverPath = "/images/upload/" . basename($localPath);
	var_dump($serverPath);
	
	if ( !move_uploaded_file( $_FILES[$name]["tmp_name"], $localPath ) )
		throw new Exception('File upload error');

	return $serverPath;
}

?>